
/**
 * @author Jason Pratt
 */

package edu.cmu.cs.stage3.alice.authoringtool.viewcontroller;

import edu.cmu.cs.stage3.resourceBundle.I18n;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringTool;
import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;
import edu.cmu.cs.stage3.alice.authoringtool.datatransfer.TransferableFactory;
import edu.cmu.cs.stage3.util.StringObjectPair;

public class PropertyDnDPanel extends edu.cmu.cs.stage3.alice.authoringtool.util.DnDGroupingPanel implements edu.cmu.cs.stage3.alice.authoringtool.util.GUIElement, edu.cmu.cs.stage3.alice.authoringtool.util.Releasable {
 protected AuthoringTool authoringTool;
 protected edu.cmu.cs.stage3.alice.core.Property property;
 protected javax.swing.JLabel nameLabel = new javax.swing.JLabel();
 protected java.util.Vector popupStructure = new java.util.Vector();

 public PropertyDnDPanel () {
  setBackground( AuthoringToolResources.getColor( "propertyDnDPanel" ) );

  add( nameLabel, java.awt.BorderLayout.CENTER );
  addDragSourceComponent( nameLabel );

  java.awt.event.MouseListener mouseListener = new edu.cmu.cs.stage3.alice.authoringtool.util.CustomMouseAdapter() {
   //Rem @Override
   public void popupResponse (java.awt.event.MouseEvent ev) {
    PropertyDnDPanel.this.updatePopupStructure();
    edu.cmu.cs.stage3.alice.authoringtool.util.PopupMenuUtilities.createAndShowPopupMenu( popupStructure, PropertyDnDPanel.this, ev.getX(), ev.getY() );
    }
   };
  addMouseListener( mouseListener );
  nameLabel.addMouseListener( mouseListener );
  grip.addMouseListener( mouseListener );
  }

 public void set (AuthoringTool authoringTool, edu.cmu.cs.stage3.alice.core.Property property) {
  this.authoringTool = authoringTool;
  this.property = property;
  nameLabel.setText( AuthoringToolResources.getReprForValue( property ) );
  String iconName = "types/" + property.getValueClass().getName();
  javax.swing.ImageIcon icon = AuthoringToolResources.getIconForValue( iconName );
  if (icon == null) {
   icon = AuthoringToolResources.getIconForValue( "types/other" );
   }
  if (icon != null) {
   //   nameLabel.setIcon( icon );
   }
  setTransferable( TransferableFactory.createTransferable( property ) );
  }

 public void updatePopupStructure () {
  popupStructure.clear();

  final edu.cmu.cs.stage3.alice.authoringtool.util.WatcherPanel watcherPanel = authoringTool.getWatcherPanel();

  if (watcherPanel.isPropertyBeingWatched( property )) {
   popupStructure.add( new StringObjectPair(I18n.getString("pdndp_stopWatching"), new Runnable() { // "stop watching this property"
    public void run() {
     watcherPanel.removePropertyBeingWatched( PropertyDnDPanel.this.property );
     }
    } ) );
  } else {
   popupStructure.add( new StringObjectPair(I18n.getString("pdndp_watchThis"), new Runnable() { // "watch this property"
    public void run () {
     watcherPanel.addPropertyToWatch( PropertyDnDPanel.this.property );
     }
    } ) );
   }
  }

 public void goToSleep () {}
 public void wakeUp () {}

 public void clean () {
  setTransferable( null );
  }

 public void die () {
  clean();
  }

 //Rem @Override
 public void release () {
  edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.releaseGUI( this );
  }

 }
